package com.lzw.spring_cache_redis.controller;

import com.lzw.spring_cache_redis.pojo.User;
import com.lzw.spring_cache_redis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @program: spring_cache_redis
 * @description: 使用用户控制器测试缓存注解
 * @author: lizhiwei
 * @create: 2019-06-13 23:57
 **/
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService = null;

    @RequestMapping("getUser")
    @ResponseBody
    public User getUser(Long id){
        return userService.getUser(id);
    }

    @RequestMapping("/insertUser")
    @ResponseBody
    public User insertUser(String userName,String note){
        User user = new User();
        user.setUserName(userName);
        user.setNote(note);
        userService.inserUser(user);
        return user;
    }

    @RequestMapping("/findUsers")
    @ResponseBody
    public List<User> findUsers(String userName, String note){
        return userService.findUsers(userName,note);
    }

    @RequestMapping("/updateUserName")
    @ResponseBody
    public String updateUserName(Long id, String userName){
        User user = userService.updateUserName(id,userName);
        boolean flag = user != null;
        String message = flag ? "更新成功" : "更新失败";
        return message;
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public String deleteUser(Long id){
        int result = userService.deleteUser(id);
        boolean flag = result == 1;
        String message = flag ? "删除成功" : "删除失败";
        return message;
    }

}
